package magnus.minimap;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;

import android.os.Handler;
import android.os.Message;

public class Connection implements Runnable { // Runnable typ som thread
	public static final int STARTED = 0;
	public static final int ERROR = 1;
	public static final int DONE = 2;

	private static final int GET = 0;
	private static final int POST = 1;
	private static final int PUT = 2;

	private String url;
	private int method;
	private Handler handler;
	private String data;
	private List<NameValuePair> postdata;

	private HttpClient httpClient;

	// Constructors
	public Connection () 					{this(new Handler());} // maybe public handler instead?
	public Connection (Handler handler) 	{this.handler = handler;}

	// Some nice http requests
	public void get		(String url) 				{registerConnection(GET, url, null,null);	}
	public void post	(String url, List<NameValuePair> postdata) 	{registerConnection(POST, url, null, postdata);	}
	public void put		(String url, String data) 	{registerConnection(PUT, url, data, null);	}
	
	// Helper function, registers current runnable with manager
	private void registerConnection (int method, String url, String data, List<NameValuePair> postdata) {
		this.method = method;
		this.url = url;
		this.data = data;
		this.postdata = postdata;
		ConnectionManager.getInstance().add2Queue(this); // Send this instance to connection manager!
	}

	// Called when ConnectionManager says go!
	public void run() {
		handler.sendMessage(Message.obtain(handler, Connection.STARTED));
		httpClient = new DefaultHttpClient();
		HttpConnectionParams.setSoTimeout(httpClient.getParams(), 25000); // Set Timeout parameter..
		try {
			HttpResponse response = null;
			switch (method) {
			case GET:
				response = httpClient.execute(new HttpGet(url));
				break;
			case POST:
				HttpPost httpPost = new HttpPost(url);
				httpPost.setEntity(new UrlEncodedFormEntity(postdata));
			    response = httpClient.execute(httpPost);
				break;
			case PUT:
				HttpPut httpPut = new HttpPut(url);
				httpPut.setEntity(new StringEntity(data));
				response = httpClient.execute(httpPut);
				break;
			}
			processEntity(response.getEntity());
		} catch (Exception e) {
			handler.sendMessage(Message.obtain(handler,
					Connection.ERROR, e));
		}
		ConnectionManager.getInstance().connectionComplete(this);
	}

	private void processEntity(HttpEntity entity) throws IllegalStateException,IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
		String line, result = "";
		while ((line = br.readLine()) != null) result += line;
		Message message = Message.obtain(handler, Connection.DONE, result);
		handler.sendMessage(message);
	}
}
